<template>
  <fks-dialog
    :visible="true"
    @close="close"
  >
    <div slot="title">
      <i class="fks-icon-plus" />
      {{ LANG.ADD_STAFF }}
    </div>
    <fks-form
      ref="Form"
      :model="form"
      label-width="85px"
    >
      <fks-form-item
        :span="12"
        prop="maintainer"
        :label="LANG.USER"
        lead
        required
      >
        <fks-user-selector
          url
          width="100%"
          multiple
          :username.sync="form.maintainer"
          :userfullname.sync="form.maintainerName"
          @change="addMembers"
        />
      </fks-form-item>
      <fks-form-item
        :span="12"
        prop="deptName"
        :label="LANG.DEPARTMENT"
      >
        <fks-org-selector
          url-path="/sys-user/orgs/tree"
          :dept-id.sync="form.deptId"
          :dept-name.sync="form.deptName"
          :params="{ portalId: portalId }"
          :readonly="true"
          :multi-index="[0]"
        />
      </fks-form-item>
    </fks-form>
    <div slot="footer">
      <fks-button
        type="primary"
        icon="fks-icon-check"
        :loading="loading"
        @click="save"
      >
        <span style="margin-left: 4px;">{{ LANG.SAVE }}</span>
      </fks-button>
    </div>
  </fks-dialog>
</template>
<script>
import * as User from '../api'

export default {
  name: 'ImportUser',
  props: {
    orgData: {
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: {},
      addUserIdList: [],
      deleteUserIdList: [],
      entName: '',
      id: '',
      loading: false
    }
  },
  computed: {
    portalId() {
      return this.$store.state.portal.id
    }
  },
  watch: {
    orgData: {
      deep: true,
      immediate: true,
      handler(val) {
        this.form.deptId = val.content.orgNo
        this.form.deptName = val.content.name
        this.id = val.id
        this.entName = val.content.entName
      }
    }
  },
  methods: {
    //关闭弹窗
    close() {
      this.$emit('update:visible', false)
    },
    //选择人员
    addMembers(userList) {
      this.deleteUserIdList = []
      this.addUserIdList = []
      if (userList) {
        userList.forEach((item) => {
          this.addUserIdList.push(item.id)
        })
      }
    },
    //添加人员
    save() {
      this.$refs.Form.validate((valid) => {
        if (valid) {
          this.loading = true
          let params = {
            addUserIdList: this.addUserIdList,
            orgNo: this.form.deptId,
            portalId: this.portalId
          }
          User.updateOrg(params)
            .then((res) => {
              this.loading = false
              if (res.status) {
                this.$message.success(this.LANG.EDIT_SUCCESS)
                this.$emit('searchUser')
                this.close()
              }
            })
            .catch((error) => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .fks-dialog__body {
  padding-bottom: 0;
}
</style>
